# -*- coding: utf-8 -*-
"""
Created on Wed Jun 27 18:39:01 2018

@author: Luther
"""

import pandas as pd
from pandas.errors import ParserError
import csv
import os


def save2utf(path):
    with open(path, 'r', encoding='utf-8', errors='ignore') as f:
        text = f.read()
    with open(path, 'w', encoding='utf-8', errors='ignore') as f:
        f.write(text)


def inspectEOF(path):
    with open(path, 'r', encoding='utf-8') as f:
        text = f.readlines()
        n = text[-1].count('"')
        if n % 2 != 0:
            endline = text[-1].replace('\n', '') + '"'
            text.pop()
            text.append(endline)
        else:
            raise ValueError
    with open(path, 'w', encoding='utf-8') as f:
        f.writelines(text)


path = input("Please input csv folder:")

file_list = os.listdir(path)

df_ret = pd.DataFrame()

for i in range(len(file_list)):
    file_path = os.path.join(path, file_list[i])
    print(file_list[i])
    try:
        df = pd.read_csv(
            file_path,
            error_bad_lines=False,
            quoting=csv.QUOTE_NONNUMERIC,
            encoding='utf-8')
        print("Success!")
    except UnicodeDecodeError:
        save2utf(file_path)
        df = pd.read_csv(
            file_path,
            error_bad_lines=False,
            quoting=csv.QUOTE_NONNUMERIC,
            encoding='utf-8')
        print("Success!")
    except ParserError:
        try:
            inspectEOF(file_path)
            df = pd.read_csv(
                file_path,
                error_bad_lines=False,
                quoting=csv.QUOTE_NONNUMERIC,
                encoding='utf-8')
            print("Success!")
        except ValueError:
            print("Unknown ParserError!")
    df_ret = df_ret.append(df, ignore_index=True)

df_ret.to_csv(os.path.join(path, "results.csv"), index=False)
